package ext.generic.workflow;

import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
import org.apache.commons.lang.StringUtils;
import ext.com.iba.IBAUtil;
import ext.com.workflow.WorkflowUtil;
import ext.generic.part.PartCommands;
import ext.generic.util.Generic;
import ext.tianma.part.forms.TMTypeUtil;
import ext.tianma.workflow.BomWorkFlowUtil;
import ext.workflow.WorkFlowUtil;
import wt.doc.WTDocument;
import wt.fc.PersistenceHelper;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.project.Role;
import wt.team.Team;
import wt.team.TeamManaged;
import wt.team.TeamReference;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.workflow.engine.WfProcess;

public class WorkflowSetParticipantsUtil {
	// workflowParticipant.properties文件路径
	private static String filePath = "/ext/generic/workflow/workflowParticipant.properties";

	/*
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setSQEParticipants(Object obj, String role, WTPart part) {

		if (obj == null || role == null || part == null) {
			return;
		}
		try {
			// 获取物料是否专显的属性
			String specialMonitor = ((String) IBAUtil.getIBAValue(part,
					"specialMonitor")).trim();
			// 物料名称
			String partName = part.getName().toString().trim();
			// 物料生效地
			String[] siteArray = ext.tianma.part.PartCommands
					.getEffectiveSiteOfPart(part);
			for (String site : siteArray) {
				String contact = partName + "." + specialMonitor + "." + site;
				System.out.println("setSQEParticipants method contact = "
						+ contact);
				// 读取配置文件的users
				Object userNames = Generic.getStrFromProperties(contact,
						filePath);
				System.out.println("setSQEParticipants method userNames = "
						+ userNames);
				if (userNames != null
						&& StringUtils.isNotBlank(userNames.toString())) {
					String[] userNameArray = userNames.toString().split(",");
					System.out
							.println("setSQEParticipants method userNameArray = "
									+ userNameArray);
					for (String userName : userNameArray) {
						System.out
								.println("setSQEParticipants method userName = "
										+ userName);
						Vector userList = PartCommands.getUserList(userName
								+ "*");
						System.out
								.println("setSQEParticipants method userList = "
										+ userList.toString());
						WorkflowUtil.addPrincipalToProcessRole(obj, role,
								(WTUser) (userList.get(0)));
					}
				}
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * @description 为武汉厦门DCC自动分配审核人员
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setDOCParticipants(Object obj, String role,
			WTDocument doc) {
		if (obj == null || role == null || doc == null) {
			return;
		}
		try {
			String developedLand = (String) IBAUtil.getIBAValue(doc,"modulesDevelopedLand");
			String secrecy = (String) IBAUtil.getIBAValue(doc, "secrecy");
			String drawingToWHG6 = (String) IBAUtil.getIBAValue(doc, "DrawingToWHG6");
			System.out.println("developedLand=" + developedLand+"secrec= " + secrecy+"drawingToWHG6="+drawingToWHG6);
			String contact = "";
			//满足 DataAdministrator.研发地.保密.项目  by - lzl
			if(StringUtils.isNotBlank(drawingToWHG6) && StringUtils.isNotBlank(developedLand) && StringUtils.isNotBlank(secrecy)) {
				contact = "DataAdministrator" + "." + developedLand + "." + secrecy+"."+drawingToWHG6;
			}else if (StringUtils.isNotBlank(developedLand)||StringUtils.isNotBlank(secrecy)) {
				contact = "DataAdministrator" + "." + developedLand + "." + secrecy;
			}
			
			// 读取配置文件的users
			Object userNames = Generic.getStrFromProperties(contact, filePath);
			System.out.println("setDOCParticipants method userNames = "
					+ userNames);
			if (userNames != null 
					&& StringUtils.isNotBlank(userNames.toString())) {
				String userName = (String) userNames;
				Vector<WTUser> userList = PartCommands.getUserList(userName);
				System.out.println("setDOCParticipants method userNameArray = "
						+ userList.get(0).getName());
				boolean flag = BomWorkFlowUtil.addPrincipalToProcessRole(obj,
						role, userList.get(0));
				System.out.println("setDOCParticipants method flag = " + flag);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}
	/*
	 * @description 深圳地生效的原材料申请流程中增加会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setCountersignParticipants(Object obj, String role, WTPart part) {
		
		if(obj == null || role == null || part == null) {
			return;
		}
		try {
			String partCName = (String)IBAUtil.getIBAValue(part, "partCName");
			Vector effectiveSite = (Vector)IBAUtil.getIBAValue2(part, "partNumberEffectiveSite");
			System.out.println("effectiveSite = " + effectiveSite);
			String contact = "";
			if (effectiveSite != null && !"".equals(effectiveSite)) {
				contact =partCName + ".HeadAssessor";
			}
			// 读取配置文件的users
			Object userNames = Generic.getStrFromProperties(contact, filePath);
			System.out.println("setCountersignParticipants method userNames = "
					+ userNames);
			if (userNames != null
					&& StringUtils.isNotBlank(userNames.toString())) {
				String userName = (String) userNames;
				Vector<WTUser> userList = PartCommands.getUserList(userName);
				System.out.println("setCountersignParticipants method userNameArray = "
						+ userList.get(0).getName());
				boolean flag = WorkflowUtil.addPrincipalToProcessRole(obj,
						role, userList.get(0));
				System.out.println("setCountersignParticipants method flag = " + flag);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}catch (WTException e) {
			e.printStackTrace();
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}


	}
	/*
	 * @description 不同物料生效地的原材料申请流程中分配BOM会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setBOMParticipants(Object obj, String role, WTPart part) {
		
		if(obj == null || role == null || part == null) {
			return;
		}
		try {
			Vector partNumberEffectiveSite = (Vector)IBAUtil.getIBAValue2(part, "partNumberEffectiveSite");
			Enumeration effectiveSiteEnum = partNumberEffectiveSite.elements();
			String effectiveSite = "";
			String contact = "";
			while(effectiveSiteEnum.hasMoreElements()) {
				effectiveSite = (String) effectiveSiteEnum.nextElement();
				System.out.println(effectiveSite + ",");
				if ((part != null) && (TMTypeUtil.isSemiFinishedGood(part))) {
					contact =effectiveSite +".SemiFinishedGood"+ ".BOMSigner";
				} 
				if((part != null) && (TMTypeUtil.isRawMaterial(part))) {
					contact =effectiveSite +".RawMaterial"+ ".BOMSigner";
				}
				// 读取配置文件的users
				Object userNames = Generic.getStrFromProperties(contact, filePath);
				System.out.println("setBOMParticipants method userNames = "
						+ userNames);
				if (userNames != null
						&& StringUtils.isNotBlank(userNames.toString())) {
					String userName = (String) userNames;
					Vector<WTUser> userList = PartCommands.getUserList(userName);
					System.out.println("setBOMParticipants method userNameArray = "
							+ userList.get(0).getName());
					boolean flag = WorkflowUtil.addPrincipalToProcessRole(obj,
							role, userList.get(0));
					System.out.println("setBOMParticipants method flag = " + flag);
				}
			}
			
		} catch (RemoteException e) {
			e.printStackTrace();
		}catch (WTException e) {
			e.printStackTrace();
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
	}
	/*
	 * @description 不同物料生效地的物料修改流程中分配BOM会签者
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	public static void setModificationBOM(Object obj, String role, WTPart part) {
		
		if(obj == null || role == null || part == null) {
			return;
		}
		try {
			WfProcess process = BomWorkFlowUtil.getProcess(obj);//获得对象的相关进程
			Vector partNumberEffectiveSite = (Vector)IBAUtil.getIBAValue2(part, "partNumberEffectiveSite");
			System.out.println("SLC ==成品BOM人员设置==>"+partNumberEffectiveSite);
			if(partNumberEffectiveSite.size() == 0) {
				Vector<WTUser> userList = PartCommands.getUserList("B089113");
				System.out.println("setModificationBOM method userNameArray = "
						+ userList.get(0).getName());
				
				WorkFlowUtil.addPrincipalToRole(process,role,userList,true);
				return;
			}
			Enumeration effectiveSiteEnum = partNumberEffectiveSite.elements();
			String effectiveSite = "";
			String contact = "";
			List<WTUser> userList2 = new ArrayList();
			while(effectiveSiteEnum.hasMoreElements()) {
				effectiveSite = (String) effectiveSiteEnum.nextElement();
				if ((part != null) && (TMTypeUtil.isSemiFinishedGood(part))) {
					contact =effectiveSite +".SemiFinishedGood"+ ".BOMSigner";
				} 
				if((part != null) && (TMTypeUtil.isRawMaterial(part))) {
					contact =effectiveSite +".RawMaterial"+ ".BOMSigner";
				}
				if ((part != null) && (TMTypeUtil.isFinishedGood(part))) {
					contact =effectiveSite +".FinishedGood"+ ".BOMSigner";
				} 
				// 读取配置文件的users
				Object userNames = Generic.getStrFromProperties(contact, filePath);
				System.out.println("setModificationBOM method userNames = "
						+ userNames);
				if (userNames != null
						&& StringUtils.isNotBlank(userNames.toString())) {
					String userName = (String) userNames;
					Vector<WTUser> userList = PartCommands.getUserList(userName);
					System.out.println("setModificationBOM method userNameArray = "
							+ userList.get(0).getName());
					userList2.addAll(userList);
				}
			}
			WorkFlowUtil.addPrincipalToRole(process,role,userList2,true);
		} catch (RemoteException e) {
			e.printStackTrace();
		}catch (WTException e) {
			e.printStackTrace();
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (WTPropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/*
	 * @description 根据是否为保密图纸自动分配审核人员
	 * 
	 * @param obj 流程对象
	 * 
	 * @param role 流程对象中的角色
	 * 
	 * @param part 流程对象中的主对象部件
	 */
	/*
	public static void setSecrecyDOCParticipants(Object obj, String role,
			WTDocument doc) {

		if (obj == null || role == null || doc == null) {
			return;
		}
		try {
			String secrecy = (String) IBAUtil.getIBAValue(doc,
					"secrecy");
			System.out.println("secrecy = " + secrecy);
			String contact = "";
			if (StringUtils.isNotBlank(secrecy)) {
				contact = "DataAdministrator" + "." + secrecy;
			}
			// 读取配置文件的users
			Object userNames = Generic.getStrFromProperties(contact, filePath);
			System.out.println("setSecrecyDOCParticipants method userNames = "
					+ userNames);
			if (userNames != null
					&& StringUtils.isNotBlank(userNames.toString())) {
				String userName = (String) userNames;
				Vector<WTUser> userList = PartCommands.getUserList(userName);
				System.out.println("setSecrecyDOCParticipants method userNameArray = "
						+ userList.get(0).getName());
				boolean flag = BomWorkFlowUtil.addPrincipalToProcessRole(obj,
						role, userList.get(0));
				System.out.println("setSecrecyDOCParticipants method flag = " + flag);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}
	*/
	
}
